package com.wuliupai.delivery.wuliupaidelivery.utils;

import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Descriptions：字符串工具类
 * <p>
 * Author：ChenME
 * Date：11/14/2016
 * Email：ibelieve1210@163.com
 */
public class
StringUtils {

    public static final int SPLIT_TYPE_1 = 1;//分为3,4,4等等
    public static final int SPLIT_TYPE_2 = 2;//分为4,4,4等等

    /**
     * 判断字符串是否为空
     *
     * @param string
     * @return 为空，返回true，
     * 不为空，返回false
     */
    public static boolean isEmpty(String string) {
        if (null == string || "".equals(string) || "null".equals(string)) {
            return true;
        }
        return false;
    }

    public static boolean isEmpty(String... strs) {
        boolean state = false;
        for (String str : strs) {
            state = null == str || "".equals(str) || "null".equals(str);
        }
        return state;
    }

    /**
     * 将字符串分组
     *
     * @param str   将要被分组的字符串
     * @param split 分隔符
     * @param type  分组类型
     * @return 已分组的字符串
     */
    public static String splitString(String str, String split, int type) {
        StringBuilder builder = new StringBuilder();
        int firstSplitLength = 3;
        if (type == SPLIT_TYPE_1) {
            firstSplitLength = 3;

        } else if (type == SPLIT_TYPE_2) {
            firstSplitLength = 4;
        }
        if (str.length() > firstSplitLength) {
            builder.append(str.substring(0, firstSplitLength)).append(split);
        } else {
            return str;
        }
        int start = firstSplitLength;
        int end = firstSplitLength + 4;
        while (end < str.length()) {
            builder.append(str.substring(start, end)).append(split);
            start = end;
            end += 4;
        }
        if (end >= str.length()) {
            builder.append(str.substring(start, str.length()));
        }
        return builder.toString();
    }

    /**
     * 将字符串按一定长度（len）分割到List中
     *
     * @param info 将要被分割的字符串
     * @param len  分割时的长度
     * @return
     */
    public static List<String> splitStringByLength(String info, int len) {
        List<String> resultList = new ArrayList<>();
        if (null == info || info.length() <= 0) {
            return resultList;
        }
        int end = len;
        String s;
        for (int start = 0; start < info.length(); ) {
            if (end > info.length()) {
                end = info.length();
            }
            s = info.substring(start, end);
            start = end;
            end = end + len;
            resultList.add(s);
        }
        return resultList;
    }

    /**
     * 将人民币的单位由分转换成元
     *
     * @param fen 被转换的金额（单位：分；数据类型：String）
     * @return
     */
    public static String format2Yuan(String fen) {
        long fenLong = 0;

        if (isEmpty(fen)) {
            return fenLong + "";
        }

        try {
            fenLong = Long.parseLong(fen);
        } catch (Exception e) {
            e.printStackTrace();
            return fenLong + "";
        }

        return format2Yuan(fenLong);
    }

    /**
     * 将人民币的单位由分转换成元
     *
     * @param fenLong 被转换的金额（单位：分；数据类型：long）
     * @return
     */
    @NonNull
    public static String format2Yuan(long fenLong) {
        StringBuilder builder = new StringBuilder();
        builder.append(fenLong / 100);
        int end = (int) fenLong % 100;
        if (end > 0 && end < 10) {
            builder.append(".").append("0").append(end);
        } else if (end >= 10) {
            builder.append(".").append(end);
        }
        return builder.toString();
    }

    /**
     * 删除float字符串中的多余0
     *
     * @param num 将要被格式化的数字字符串
     * @return
     */
    public static String dropNeedlessZeroOfNum(String num) {
        if (num.indexOf(".") > 0) {
            //正则表达
            num = num.replaceAll("0+?$", "");//去掉后面无用的零
            num = num.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
        }
        return num;
    }

    /**
     * 格式化体积
     *
     * @param volume
     * @return
     */
    public static String formatVolume(String volume) {
        if (isEmpty(volume)) {
            return "0方";
        }
        return dropNeedlessZeroOfNum(volume) + "方";
    }

    /**
     * 格式化重量
     *
     * @param weight
     * @return
     */
    public static String formatWeight(String weight) {
        if (isEmpty(weight)) {
            return "0Kg";
        }
        String result = "";
        try {
            double w = Double.valueOf(weight);
            if (w >= 1000) {
                w = NumberUtils.formatDouble(w / 1000, "0.0");
                result = dropNeedlessZeroOfNum(w + "") + "吨";
            } else {
                w = NumberUtils.formatDouble(w, "0.0");
                result = dropNeedlessZeroOfNum(w + "") + "Kg";
            }
            return result;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return "0 Kg";
        }
    }
}